<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增加班申请')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-overtime-add">
            <div class="form-group"  style="display: none" >
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>部门：</label>
                <div class="col-sm-8">
                    <select name="deptId" class="form-control m-b" th:with="deptAll=${@deptService.getDeptAll()}" disabled="disabled">
                        <option value="">请选择</option>
                        <option th:each="dept : ${deptAll}" th:text="${dept.getShowName()}" th:value="${dept.getDeptId()}" th:selected="${dept.getDeptId() == hrEmp?.deptId}" th:hidden="${dept.getStatus() == '1' || dept.getDelFlag() != '0' }"></option>
                    </select>
                </div>
            </div>
            <div class="form-group" style="display: none" >
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>岗位：</label>
                <div class="col-sm-8">
                    <select name="postId" class="form-control m-b" th:with="postAll=${@postService.getPostAll()}" disabled="disabled">
                        <option value="">请选择</option>
                        <option th:each="post : ${postAll}" th:text="${post.getPostName()}" th:value="${post.getPostId()}" th:selected="${post.getPostId() == hrEmp?.postId}" th:hidden="${post.getStatus() == '1'}"></option>
                    </select>
                </div>
            </div>

          <!--  <div class="form-group">
                <label class="col-sm-3 control-label is-required">加班人员：</label>
                <div class="col-sm-8">
                    <div class='input-group'>
                        <input class='form-control' type='hidden' name='empIds' id='empIds' required>
                        <input class='form-control' type='hidden' name='applyUser' id='applyUser' required>
                        <input class='form-control' type='hidden' name='applyUserName' id='applyUserName' required>
                        <textarea class='form-control' rows="3" type="text" name='empName' id='empName' readonly="readonly" required></textarea>
                        <span class='input-group-addon input-sm' onclick='selectEmpTree("", this)'>
                            <i class='fa fa-search'>选择</i>
                        </span>
                    </div>
                </div>
            </div>-->


            <div class="form-group">    
                <label class="col-sm-3 control-label">加班类型：</label>
                <div class="col-sm-8">
                    <select name="type" class="form-control m-b" th:with="type=${@dict.getType('overtime_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">原因：</label>
                <div class="col-sm-8">
                    <textarea name="reason" class="form-control"></textarea>
                    <input name="title" id="title" class="form-control" type="text" style="display: none;" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="startTime" class="form-control calcTotalSecondInput" readonly="readonly" placeholder="yyyy-MM-dd" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="endTime" class="form-control calcTotalSecondInput" readonly="readonly" placeholder="yyyy-MM-dd" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">加班时长：</label>
                <div class="col-sm-8">
                    <input name="totalTimes" class="form-control" readonly="readonly" type="hidden" required>
                    <input name="totalTimeText" class="form-control" type="text" readonly>
                </div>
            </div>

        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script type="text/javascript">
        var prefix = ctx + "hr/overtime"
        $("#form-overtime-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form() && validateStartTimeAndEndTime()) {
                $('input[name="title"]').val('[[${hrEmp?.empName}]]加班申请');
                $("select[name=deptId]").attr("disabled",false);
                $("select[name=postId]").attr("disabled",false);
                $.operate.save(prefix + "/add", $('#form-overtime-add').serialize());
            }
        }

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: "hour",
            autoclose: true
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: "hour",
            autoclose: true
        });

        // 监听开始和结束日期填写，动态生成请假时长
        $(".calcTotalSecondInput").change(function () {

            $.ajax({
                type: 'POST',
                url: prefix + "/getOverTime",
                data: $('#form-overtime-add').serialize(),
                dataType: 'json',
                success: function (result) {
                    if (result.msg == "success") {
                        // console.log(result.data);
                        console.log(result.data["hours"])
                        $('input[name="totalTimeText"]').val(result.data["hours"] + " 小时");
                        $('input[name="totalTimes"]').val(result.data["hours"]);
                    } else {
                        console.log(result.data)
                        $('input[name="totalTimeText"]').val(0 + " 小时");
                    }
                },
                error: function (error) {
                    $.modal.alertWarning("系统错误，请联系管理员");
                }


            });

            //Datechange();
        });

        function validateStartTimeAndEndTime() {
            if ($('input[name="startTime"]').val() > $('input[name="endTime"]').val()) {
                $.modal.alertWarning("结束时间必须大于开始时间");
                return false;
            }
            return true;
        }


        function selectEmpTree(columnId, obj) {
            var url = ctx + "system/user/selectUserTreeCheckbox?msg=最多添加50位成员&count=50";
            var options = {
                title: '选择成员',
                width: "380",
                url: url,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index){
            // debugger
            var body = layer.getChildFrame('body', index);
            var empIds = body.find('#userId').val();
            var userNames = body.find('#userName').val();
            var empNames = body.find('#empName').val();
            layer.close(index);
            $("#empIds").val(empIds);
            $("#applyUser").val(userNames);
            $("#applyUserName").val(empNames);
            $("#empName").val(empNames);
        }

    </script>
</body>
</html>